package com.zzk.protect.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zzk.protect.common.CommonResult;
import com.zzk.protect.pojo.ExaminationApplication;
import com.zzk.protect.service.ExaminationApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/examApp")
public class ExamAppController {
    @Autowired
    private ExaminationApplicationService service;
    @GetMapping("/list")
    public CommonResult<List<ExaminationApplication>> list(
            @RequestParam Integer registrationId) {
        QueryWrapper<ExaminationApplication> qw = new QueryWrapper<>();
        qw.eq("registration_id", registrationId);
        return CommonResult.success(service.list(qw));
    }
    @PostMapping("/saveBatch")
    public CommonResult  saveBatch(@RequestBody List<ExaminationApplication> applications ){
        Date now = new Date();
        for (ExaminationApplication app : applications) {
            app.setCreateTime(now); // 确保实体类中有 setCreateTime(Date) 方法
        }
        return CommonResult.success(service.saveBatch(applications));
    }
    @PostMapping("/deleteBatch")
    public CommonResult deleteBatch(@RequestBody List<Long> ids){
        return CommonResult.success(service.removeByIds(ids));
    }
    @PostMapping("/updateStatus")
    public CommonResult updateStatus(@RequestParam Integer registrationId, @RequestParam Integer status) {
        try {

            UpdateWrapper<ExaminationApplication> updateWrapper = new UpdateWrapper<>();


            updateWrapper.eq("registration_id", registrationId);

            // 创建要更新的实体对象，设置status字段
            ExaminationApplication updateEntity = new ExaminationApplication();
            updateEntity.setStatus(status);
            boolean success = service.update(updateEntity, updateWrapper);

            if (success) {
                return CommonResult.success("状态更新成功");
            } else {
                return CommonResult.failed("更新失败，未找到匹配的记录");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("更新状态时发生错误：" + e.getMessage());
        }
    }
}
